<div class="row-fluid">
	<div class="span12">
		<ul class="breadcrumb">
			<li>
				<a href="index.php">
					<i class="radmin-icon radmin-home"></i>
					<?php echo t('page', 'Dashboard'); ?>
				</a>
				<span class="divider">/</span>
			</li>
			<li class="active">
				<i class="radmin-icon radmin-user"></i>
				<?php echo t('page', 'User List'); ?>
			</li>
		</ul>
	</div>
</div>

<div class="row-fluid">
	<div class="span12 hide" id="user_list_error_info">
		<div class="alert alter-block alert-error">
<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
			<h4>Error</h4>
			<div class="content"></div>
		</div>
	</div>
</div>

<div class="row-fluid">
	<div class="span9">
		<h4 class="title"><?php $this->widget('application.components.mywidgets.WikiHelp', array('wiki' => 'user_list')); ?> <?php echo t('page', 'User List'); ?></h4>
		<div class="squiggly-border"></div>
		
		<?php $this->widget('zii.widgets.grid.CGridView', array(
			'id' => 'user_grid',
			'summaryText' => '',
			'dataProvider' => $user->search(),
			'columns' => array(
				array(
					'header' => 'Id',
					'value' => '$data->id_user',
					'headerHtmlOptions' => array('class' => 'hide'),
					'htmlOptions' => array('class' => 'hide id_user')
				),
				array(
					'header' => t('page', 'Username'),
					'value' => '$data->username',
					'htmlOptions' => array('class' => 'username')
				),
				array(
					'header' => t('page', 'Name'),
                    'value' => '$data->fullname',
					'headerHtmlOptions' => array('class' => 'hidden-phone'),
					'htmlOptions' => array('class' => 'hidden-phone fullname')
				), 
				array(
					'header' => t('page', 'Email'),
					'value' => '$data->email',
					'htmlOptions' => array('class' => 'email')
				),
				array(
					'header' => t('page', 'Role'),
					'value' => 'empty($data->Role)? "" : $data->Role->name',
					'headerHtmlOptions' => array('class' => 'hidden-phone'),
					'htmlOptions' => array('class' => 'hidden-phone role')
				),
				array(
					'header' => t('page', 'Status'),
					'value' => '$data->statusTxt',
					'htmlOptions' => array('class' => 'status')
				),
				array(
					'header' => t('page', 'Action'),
					'type' => 'raw',
					'value' => '$data->renderListActionButtons()'
				)
			),
			'itemsCssClass' => 'table table-index table-striped',
			'pagerCssClass' => 'pagination pagination-right',
			'pager' => array(
				'class' => 'LinkPager',
				
				
				
			),
			'htmlOptions' => array(
				'class' => ''
			)
		));?>
	</div>

	<?php $this->renderPartial('forms/user/_add', array('userAddForm' => $userAddForm)); 
	 $this->renderPartial('forms/user/_edit', array('userEditForm' => $userEditForm)); ?>
	<script>
		$(document).ready(function(){
			$('#user_grid .user_edit').live('click', function(){
				var id_user = $(this).parents('tr').find('.id_user').text();
				$('.user_edit-line').removeClass('user_edit-line');
				$(this).parents('tr').addClass('user_edit-line');
				$.ajax({
					url: <?php echo json_encode(url('user/detail')); ?>,
					type: 'get',
					data: {id_user: id_user},
					success: function(detail){
						$('#user-edit-form .fields').val('');
						$.each(detail, function(key, value){
							if($('#user-edit-form #UserEditForm_'+key)){
								$('#user-edit-form #UserEditForm_'+key).val(value);
							}
						});
						$('#user-edit-form #UserEditForm_password').val('');
						$('#user-edit-form #UserEditForm_password_confirm').val('');
						
						$('#user-add-block').fadeOut(function(){
							$('#user-edit-block').fadeIn();
						});
					},
					dataType: 'json'
				});
				
			});

			$('#user_grid .user_delete').live('click', function(){
				$(this).parents('.btn-group').find('.actions').hide();
				$(this).parents('.btn-group').find('.delete_confirm_btn').show();
			});
			$('#user_grid .delete_cancel').live('click', function(){
				$(this).parents('.btn-group').find('.actions').show();
				$(this).parents('.btn-group').find('.delete_confirm_btn').hide();
			});
			$('#user_grid .delete_confirm').live('click', function(){
				var obj = $(this);
				$(this).parents('.btn-group').find('button.actions').attr('data-loading-text', 'deleting').button('loading');
				var id_user = $(this).parents('tr').find('.id_user').text();
				$.ajax({
					url: '<?php echo url('user/delete'); ?>',
					type: 'post',
					data: {id_user: id_user},
					success: function(ret){
						obj.parents('.btn-group').find('button.actions').button('reset');
						if(ret.result){
							obj.parents('tr').remove();
						}else{
							$("#user_list_error_info").blink({
								"beforeCall": function(){
									$("#user_list_error_info .content").html("");
									$.each(ret["errors"], function(attrName, error){
										$(error).each(function(k, e){
											var p = $("<p />").addClass("text-error").append(attrName+": "+e);
											$("#user_list_error_info .content").append(p);
										});
									});
								}
							});
						}
						obj.parents('.btn-group').find('.delete_cancel').click();
					},
					dataType: 'json'
				});
			});

			$('#user_grid .user_authy').live('click', function(){
				if($(this).children('a').attr('href') == 'javascript:void(0);'){
					var id_user = $(this).parents('tr').find('.id_user').text();
					if($(this).children('a').attr('id') == 'setup'){
						window.location.href = '<?php echo url('user/authy'); ?>/id_users[]/'+id_user;
					}else{
						$(this).parents('.btn-group').find('.actions').hide();
						$(this).parents('.btn-group').find('.disableauthy_confirm_btn').show();
					}
				}

				return true;
			});
			
			$('#user_grid .disableauthy_cancel').live('click', function(){
				$(this).parents('.btn-group').find('.actions').show();
				$(this).parents('.btn-group').find('.disableauthy_confirm_btn').hide();
			});

			$('#user_grid .disableauthy_confirm').live('click', function(){
				var id_user = $(this).parents('tr').find('.id_user').text();
				$(this).parents('.btn-group').find('button.actions').attr('data-loading-text', 'Disable authy...').button('loading');
				var obj = $(this);
				$.ajax({
					url: '<?php echo url('user/disableauthy'); ?>',
					type: 'post',
					data: {id_user: id_user},
					success: function(ret){
						obj.parents('.btn-group').find('button.actions').button('reset');
						obj.parents('.btn-group').find('.disableauthy_cancel').click();
						if(ret.result){
							obj.parents('.btn-group').find('.user_authy a').attr('id', 'setup');
							obj.parents('.btn-group').find('.user_authy a').text('Setup 2-Factor Auth');
						}else{
							$("#user_list_error_info").blink({
								"beforeCall": function(){
									$("#user_list_error_info .content").html("");
									$.each(ret["errors"], function(attrName, error){
										$(error).each(function(k, e){
											var p = $("<p />").addClass("text-error").append(attrName+": "+e);
											$("#user_list_error_info .content").append(p);
										});
									});
								}
							});
						}
						obj.parents('.btn-group').find('.delete_cancel').click();
					},
					dataType: 'json'
				});
			});

			$('#switch-user-add-btn').click(function(){
				$('#user-edit-block').fadeOut(function(){
					$('#user-add-block').fadeIn();
				});
			});
		});
	</script>
</div>